home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / bf_test.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  4KB  |  164 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: bf_test.c%v 3.50 1993/07/09 05:35:24 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /*
  7.  * Test routines for binary files
  8.  * cc bf_test.c -o bf_test binary_files.o -lm
  9.  *
  10.  * Copyright (c) 1992 Robert K. Cunningham, MIT Lincoln Laboratory
  11.  *
  12.  */
  13. #include <stdio.h>
  14. #if !defined(apollo) && !defined(sequent) && !defined(u3b2) && !defined(alliant) && !defined(sun386)
  15. #include <stdlib.h>
  16. #else
  17. extern char *malloc();
  18. #endif
  19. #include <math.h>
  20. float *vector();
  21. float *extend_vector();
  22. float *retract_vector();
  23. float **matrix();
  24. float **extend_matrix();
  25. float **retract_matrix();
  26. void free_matrix();
  27. void free_vector();
  28.  
  29. typedef struct {
  30.   float xmin, xmax;
  31.   float ymin, ymax;
  32. } range;
  33.  
  34. #define NUM_PLOTS 2
  35. range TheRange[] = {{-3,3,-2,2},
  36.                     {-3,3,-3,3},
  37.                     {-3,3,-3,3}};/* Sampling rate causes this to go from -3:6*/
  38.  
  39. /*---- Stubs to make this work without including huge libraries ----*/
  40. void int_error(error_text,dummy)
  41.      char *error_text;
  42.      int dummy;
  43. {
  44.   fprintf(stderr,"Fatal error..\n");
  45.   fprintf(stderr,"%s\n",error_text);
  46.   fprintf(stderr,"...now exiting to system ...\n");
  47.   exit(1);
  48. }
  49.  
  50. char *alloc(i)
  51. unsigned long i;
  52. {
  53.   return(malloc(i));
  54. }
  55. /*---- End of stubs ----*/
  56.  
  57. float function(p,x,y)
  58. int p;
  59. double x,y;
  60. {
  61.   float t;
  62.   switch (p){
  63.   case 0:
  64.     t = 1.0/(x*x+y*y+1.0);
  65.     break;
  66.   case 1:
  67.     t = sin(x*x + y*y)/(x*x + y*y);
  68.     if (t> 1.0)  t = 1.0;
  69.     break;
  70.   case 2:
  71.     t = sin(x*x + y*y)/(x*x + y*y);
  72.     t *= sin(4.*(x*x + y*y))/(4.*(x*x + y*y));  /* sinc modulated sinc */
  73.     if (t> 1.0)  t = 1.0;
  74.     break;
  75.   default:
  76.     fprintf(stderr, "Unknown function\n");
  77.     break;
  78.   }
  79.   return t;
  80. }
  81.  
  82. #define ISOSAMPLES (double)5
  83. int
  84. main()
  85. {
  86.   int plot;
  87.   int i,j;
  88.   float x,y;
  89.   float *rt,*ct;
  90.   float **m;
  91.   int xsize,ysize;
  92.   char buf[256];
  93.   FILE *fout;
  94. /*  Create a few standard test interfaces */
  95.  
  96.   for(plot = 0; plot < NUM_PLOTS; plot++){
  97.     xsize = (TheRange[plot].xmax - TheRange[plot].xmin)*ISOSAMPLES +1;
  98.     ysize = (TheRange[plot].ymax - TheRange[plot].ymin)*ISOSAMPLES +1;
  99.  
  100.     rt = vector(0,xsize-1);
  101.     ct = vector(0,ysize-1);
  102.     m =  matrix(0,xsize-1,0,ysize-1);
  103.  
  104.     for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  105.       ct[j] = y;
  106.     }
  107.  
  108.     for(x=TheRange[plot].xmin,i=0;i<xsize;i++,x+=1.0/(double)ISOSAMPLES){
  109.       rt[i] = x;
  110.       for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  111.         m[i][j] = function(plot,x,y);
  112.       }
  113.     }
  114.  
  115.     sprintf(buf,"binary%d",plot+1);
  116. #ifdef UNIX
  117.     if(!(fout = fopen(buf,"w")))
  118. #else
  119.     if(!(fout = fopen(buf,"wb")))
  120. #endif
  121.       int_error("Could not open file",0);
  122.     else{
  123.       fwrite_matrix(fout,m,0,xsize-1,0,ysize-1,rt,ct);
  124.     }
  125.     free_vector(rt,0,ysize-1);
  126.     free_vector(ct,0,xsize-1);
  127.     free_matrix(m,0,xsize-1,0,ysize-1);
  128.   }
  129.  
  130.   /* Show that it's ok to vary sampling rate, as long as x1<x2, y1<y2... */
  131.   xsize = (TheRange[plot].xmax - TheRange[plot].xmin)*ISOSAMPLES +1;
  132.   ysize = (TheRange[plot].ymax - TheRange[plot].ymin)*ISOSAMPLES +1;
  133.  
  134.   rt = vector(0,xsize-1);
  135.   ct = vector(0,ysize-1);
  136.   m =  matrix(0,xsize-1,0,ysize-1);
  137.  
  138.   for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  139.     ct[j] = y>0?2*y:y;
  140.   }
  141.   for(x=TheRange[plot].xmin,i=0;i<xsize;i++,x+=1.0/(double)ISOSAMPLES){
  142.     rt[i] = x>0?2*x:x;
  143.     for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  144.       m[i][j] = function(plot,x,y);
  145.     }
  146.   }
  147.  
  148.   sprintf(buf,"binary%d",plot+1);
  149. #ifdef UNIX
  150.   if(!(fout = fopen(buf,"w")))
  151. #else
  152.   if(!(fout = fopen(buf,"wb")))
  153. #endif
  154.     int_error("Could not open file",0);
  155.   else{
  156.     fwrite_matrix(fout,m,0,xsize-1,0,ysize-1,rt,ct);
  157.   }
  158.   free_vector(rt,0,ysize-1);
  159.   free_vector(ct,0,xsize-1);
  160.   free_matrix(m,0,xsize-1,0,ysize-1);
  161.  
  162.   return(0);
  163. }
  164.